package cn.edu.neusoft.cqy917.zyfypt_cqy917.model;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.bean.LoginBean;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.LoginListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.Loginiface;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.service.LoginService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class LoginModel implements Loginiface{
    private LoginService loginService;
    private Retrofit retrofit;
    private static final  String BASE_URL=
            "http://amicool.neusoft.edu.cn/";
    public LoginModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(
                        GsonConverterFactory.create())
                .build();

    }

    @Override
    public void getLoginResult(String username, String passwd,final LoginListener loginListener) {
        loginService =retrofit.create(LoginService.class);
        Call<LoginBean> call =loginService.login(username,passwd);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    loginListener.onResponse(response.body());
                else
                   loginListener.onFail("error");
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                loginListener.onFail("error");
            }
        });
    }

}
